/*
 * Copyright 2007 Stefan Arentz <http://stefan.arentz.nl>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sateh.spaces.simple;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class SimpleSpaceUtils
{
    private static final Log log = LogFactory.getLog(SimpleSpaceUtils.class);

    /** Return a Map containing field name and value tuples for all simple public fields. */

    public static Map<String,Object> getObjectProperties(Object object)
    {
        Map<String,Object> fieldWithValues = new HashMap<String,Object>();

        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(object.getClass());
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            if (isSupportedType(propertyDescriptor.getPropertyType())) {
                try {
                    Object value = propertyDescriptor.getReadMethod().invoke(object);
                    fieldWithValues.put(propertyDescriptor.getName(), value);
                } catch (IllegalAccessException e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Cannot access property " + object.getClass().getName() + "." + propertyDescriptor.getName() + ":" + e.getMessage(), e);
                    }
                } catch (InvocationTargetException e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Cannot access property " + object.getClass().getName() + "." + propertyDescriptor.getName() + ":" + e.getMessage(), e);
                    }
                }
            }
        }

        return fieldWithValues;
    }

    /** Return a Map containing non null field name and value tuples for all simple public fields. */

    public static Map<String,Object> getTemplateProperties(Object templateObject)
    {
        Map<String,Object> fields = new HashMap<String, Object>();

        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(templateObject.getClass());
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            if (isSupportedType(propertyDescriptor.getPropertyType())) {
                try {
                    Object value = propertyDescriptor.getReadMethod().invoke(templateObject);
                    if (value != null) {
                        fields.put(propertyDescriptor.getName(), value);
                    }
                } catch (IllegalAccessException e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Cannot access property " + templateObject.getClass().getName() + "." + propertyDescriptor.getName() + ":" + e.getMessage(), e);
                    }
                } catch (InvocationTargetException e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Cannot access property " + templateObject.getClass().getName() + "." + propertyDescriptor.getName() + ":" + e.getMessage(), e);
                    }
                }
            }
        }

        return fields;
    }
    
    private static final Set<Class<?>> supportedTypes = new HashSet<Class<?>>();

    static {
        supportedTypes.add(Boolean.class);
        supportedTypes.add(Byte.class);
        supportedTypes.add(Character.class);
        supportedTypes.add(Double.class);
        supportedTypes.add(Enum.class);
        supportedTypes.add(Float.class);
        supportedTypes.add(Integer.class);
        supportedTypes.add(Long.class);
        supportedTypes.add(Short.class);
        supportedTypes.add(String.class);
    }

    private static boolean isSupportedType(Class<?> clazz)
    {
        return supportedTypes.contains(clazz) || clazz.isEnum();
    }

    //

    public static void collectClassNames(Collection<String> classNames, Class clazz)
    {
        if (clazz != null) {
            classNames.add(clazz.getName());
            collectClassNames(classNames, clazz.getSuperclass());
        }
    }

    public static void collectInterfaceNames(Collection<String> interfaceNames, Class[] interfaces)
    {
        for (Class interfaze : interfaces) {
            interfaceNames.add(interfaze.getName());
            collectInterfaceNames(interfaceNames, interfaze.getInterfaces());
        }
    }
}
